local Base = _ui("base")
local Button = _ui("button")

return Class{

	Scrollbar = Class{
		init = function (self, x, y, w, c)
			lume.extend(self, Base(x, y))
			self.w = w or 6
			self.color = c or {.4, .6, .2}
		end,
		
		offset = 0,
		
		update = function (self)
			self.offset = self.offset + Game.input.wheel_move.y
			self.y = self.y + self.offset
		end,
		
		draw = function (self)
			_lg.setColor(table.unpack(self.color))
			_lg.rectangle('fill', self.x, self.y, self.w, self.h)
		end,
	},

	init = function(self, x, y, content, cb, hide)
		lume.extend(self, Base(x, y))
		if type(content) == "string" then
			self.table = _lf.getDirectoryItems(content)
		elseif type(content) == "table" then
			self.table = content
		end
		local y = self.y
		self.items = {}
		self.button_callback = cb or function () end
		lume(self.table):each(
		function (v)
			y = self:next_row(y)
			local name = pl.path.splitext(v)
			lume(self.items)
				:push(Button(self.x, y, name, 
					function (b) 
						self:button_callback(b)
					end))
		end)
		self.hide = hide
	end,
	
	get_selected = function (self)
		return self.selected
	end,
	
	update = function (self, dt)
		lume(self.items):each(function (t) 
			if t.update then
				t:update(dt)
			end
		end)
	end,
	
	next_row = function (self, y)
		if not y then return end
		local h = 60
		return y + h
	end,
	
	draw = function (self)
		if self.hide then return end
		if self.view then self.view:draw() end
		lume(self.items):each(function (t) 
			t:draw()
		end)
		
	end,
}
